from django.contrib import admin
from django.db import connection

from .models import ProvinceModel, CityModel, DepartmentModel, HospitalModel, DoctorModel, TreatmentModel, \
    DepartmentCategoryModel, OperationModel, OperationCategoryModel, DoctorEvaluationModel,QuestionModel,AnswerModel


# Register your models here.

class CityInline(admin.TabularInline):
    model = CityModel
    fields = ['id', 'name']


class ProvinceAdmin(admin.ModelAdmin):
    list_display = ['id', 'name']
    inlines = [CityInline]


admin.site.register(ProvinceModel, ProvinceAdmin)


class CityAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'province']
    list_filter = ['province', 'is_hot']
    search_fields = ['name']


admin.site.register(CityModel, CityAdmin)


class OperationCategoryAdmin(admin.ModelAdmin):
    list_display = ['id', 'name']
    search_fields = ['name']


admin.site.register(OperationCategoryModel, OperationCategoryAdmin)


class OperationAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'category', 'departments']
    list_filter = ['departments']
    search_fields = ['name']


admin.site.register(OperationModel, OperationAdmin)


class DepartmentCategoryAdmin(admin.ModelAdmin):
    list_display = ['id', 'name']
    search_fields = ['name']


admin.site.register(DepartmentCategoryModel, DepartmentCategoryAdmin)


class DepartmentAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'category']
    search_fields = ['name']


admin.site.register(DepartmentModel, DepartmentAdmin)


class HospitalAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'level', 'province', 'city', 'covers_small']
    list_filter = ['city', 'level', 'province']
    search_fields = ['name', 'address']
    # 多对多关系的字段，需要使用filter_horizontal,不然会显示成下拉框
    filter_horizontal = ['departments']


admin.site.register(HospitalModel, HospitalAdmin)


class DoctorEvaluationAdmin(admin.ModelAdmin):
    list_display = ['id', 'user', 'doctor', 'created_time', 'score']
    list_filter = ['doctor']


admin.site.register(DoctorEvaluationModel, DoctorEvaluationAdmin)


class DoctorAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'hospital', 'department', 'title', 'consultation_price', 'rating', 'avatar_preview']
    list_filter = ['title', 'hospital__province', 'hospital__city', 'department']
    search_fields = ['name', 'introduction', 'specialty']
    ordering = ['-rating']


admin.site.register(DoctorModel, DoctorAdmin)


class TreatmentAdmin(admin.ModelAdmin):
    list_display = ['id', 'name', 'department']
    list_filter = ['department']
    search_fields = ['name']
    filter_horizontal = ['doctors']


admin.site.register(TreatmentModel, TreatmentAdmin)




class QuestionAdmin(admin.ModelAdmin):
    list_display = ['id', 'department', 'treatment','view_count','created_time']
    list_filter = ['department', 'treatment']
    search_fields = ['content']
    date_hierarchy = 'created_time'

admin.site.register(QuestionModel, QuestionAdmin)

class AnswerAdmin(admin.ModelAdmin):
    list_display = ['id', 'question', 'doctor', 'created_time']
    list_filter = ['question','doctor']
    search_fields = ['content']
    date_hierarchy = 'created_time'

admin.site.register(AnswerModel, AnswerAdmin)